package hello.rest;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import hello.socket.User;

/**
 * 模拟登录
 * @author Lanxumit
 *
 */
@RestController
public class LoginController {

	public static Map<String,User> users = new HashMap<String,User>();
	
	static{
		User u1 = new User();
		u1.setId("1");
		u1.setName("name1");
		User u2 = new User();
		u2.setId("2");
		u2.setName("name2");
		users.put(u1.getId(), u1);
		users.put(u2.getId(), u2);
	}
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public boolean login(User u){
		if(users.containsKey(u.getId())){
			//处理senssion
			HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			HttpSession session  = req.getSession();
			session.setAttribute("user", u);
			return   true ;
		}else{
			return   false; 
		}
	} 
}
